package com.gxg.newdemo1.mynewdamo1.dayin;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.FileProvider;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.gxg.newdemo1.mynewdamo1.R;
import com.socks.library.KLog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * create om  2018/11/27.
 * Created by  gaoxuge
 * email android_gaoxuge@163.com
 * 功能描述
 */
public class DayinActivty extends AppCompatActivity {
    @BindView(R.id.tv_dayin)
    TextView tvDayin;
    @BindView(R.id.tv_dayin_img)
    TextView tvDayinImg;
    @BindView(R.id.tv_dayin_word)
    TextView tvDayinWord;
    private String wordPath;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dayin);
        ButterKnife.bind(this);
        String absolutePath = FileUtility.getSDPath().getAbsolutePath();
        pdfPath = absolutePath + "/1543299832374_3155.pdf";
        wordPath = absolutePath + "/aaaaa.doc";
        imgPath = FileUtility.getSDPath().getPath() + "/MagazinePic-17-2.3.001-bigpicture_17_4_land.jpg";
        KLog.e("sss  " + imgPath);
        KLog.e("sss  " + pdfPath + "\n" + absolutePath);

    }
    @OnClick({R.id.tv_dayin_img, R.id.tv_dayin_word, R.id.tv_dayin})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_dayin_img:
                printerShareWORD(DayinActivty.this,imgPath,0);
                break;
            case R.id.tv_dayin_word:
                printerShareWORD(DayinActivty.this,wordPath,1);
                break;
            case R.id.tv_dayin:
                printerShareWORD(DayinActivty.this,pdfPath,2);
                break;
        }
    }
//    @OnClick({R.id.tv_dayin,R.id.tv_dayin_img,R.id.tv_dayin_word})
//    public void onClick(View view) {
//        switch ()
////        printerShareWORD(DayinActivty.this,imgPath,0);
//        printerShareWORD(DayinActivty.this,wordPath,2);
//    }

    /**
     * 判断PrinterShare是否安装
     *
     * @param context
     * @param
     * @return
     */
    public boolean isAppInstalled(Context context) {
        String packageName = "com.dynamixsoftware.printershare";
//        String packageName = PrinterManageUtil.PRINT_SHARE_PAKEAGE_NAME;
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        List pName = new ArrayList();
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                KLog.e("sss  " + pinfo.get(i).packageName);
                pName.add(pn);
            }
        }
        return pName.contains(packageName);
    }

    /**
     * 使用PrinterShare打印Word文档
     *
     * @param context
     */
    public void printerShareWORD(Context context, String filePath, int type) {
        if (isAppInstalled(context)) {
            startPicturesPrinterShare(context, filePath, type);
        } else {
            Toast.makeText(context, "请先安装PrintShare打印工具", Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 使用PrinterShare打印图片
     *
     * @param context
     * @param filePath 图片地址
     */
    public void printerShareBitmap(Context context, String filePath) {
        if (isAppInstalled(context)) {
            startPicturesPrinterShare(context, filePath, 0);
//insertDataBase();
        } else {
            Toast.makeText(context, "请先安装PrintShare打印工具", Toast.LENGTH_SHORT).show();
        }
    }

    private String pdfPath;
    private String imgPath;

    /**
     * 启动图片打印
     *
     * @param context
     * @param context
     * @param types   0 图片 1 word 2 pdf
     */
    public void startPicturesPrinterShare(Context context, String path, int types) {
        String param = path;
        String cls ;
        String type;
        switch (types) {
            case 0:
                cls = "com.dynamixsoftware.printershare.ActivityPrintPictures";
                break;
            case 1:
                cls = "com.dynamixsoftware.printershare.ActivityPrintDocuments";
                break;
            case 2:
                cls = "com.dynamixsoftware.printershare.ActivityPrintPDF";
                break;
            default:
                cls = "com.dynamixsoftware.printershare.ActivityPrintPictures";
                break;
        }

        Intent intent = new Intent();
        ComponentName comp = new ComponentName(
                "com.dynamixsoftware.printershare", cls);
        intent = new Intent(Intent.ACTION_VIEW);
        intent.setComponent(comp);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//        intent.setAction("android.intent.action.VIEW");
        Uri fileUri = null;
        KLog.e("sss   " + param);
        if (Build.VERSION.SDK_INT >= 24) {
            fileUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", new File(param));
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            fileUri = Uri.fromFile(new File(param));
        }

        KLog.e("sss  " + param);
        KLog.e("sss  " + fileUri.toString());
//        switch (types) {
//            case 0:
//                type = "image/*";
//                intent.setDataAndType(fileUri, type);
//                break;
//            case 1:
//                type = "application/msword";
//                intent.setDataAndType(fileUri, "text/*");
//                intent.setDataAndType(fileUri, "application/vnd.ms-powerpoint");
//                intent.setDataAndType(fileUri, "application/vnd.ms-excel");
//                intent.setDataAndType(fileUri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
//                intent.setDataAndType(fileUri, type);
//                break;
//            case 2:
//                type = "application/pdf";
//                intent.setDataAndType(fileUri, "text/*");
//                intent.setDataAndType(fileUri, "application/msword");
//                intent.setDataAndType(fileUri, "application/vnd.ms-powerpoint");
//                intent.setDataAndType(fileUri, "application/vnd.ms-excel");
//                intent.setDataAndType(fileUri, type);
//                break;
//            default:
//                type = "image/*";
//                intent.setDataAndType(fileUri, type);
//                break;
//        }
        intent.putExtra( "scaleFitToPage", true );
        intent.setDataAndType(fileUri, "*/*");
        context.startActivity(intent);
    }



    /**
     *  {".3gp",    "video/3gpp"},
     {".apk",    "application/vnd.android.package-archive"},
     {".asf",    "video/x-ms-asf"},
     {".avi",    "video/x-msvideo"},
     {".bin",    "application/octet-stream"},
     {".bmp",    "image/bmp"},
     {".c",  "text/plain"},
     {".class",  "application/octet-stream"},
     {".conf",   "text/plain"},
     {".cpp",    "text/plain"},
     {".doc",    "application/msword"},
     {".docx",   "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
     {".xls",    "application/vnd.ms-excel"},
     {".xlsx",   "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
     {".exe",    "application/octet-stream"},
     {".gif",    "image/gif"},
     {".gtar",   "application/x-gtar"},
     {".gz", "application/x-gzip"},
     {".h",  "text/plain"},
     {".htm",    "text/html"},
     {".html",   "text/html"},
     {".jar",    "application/java-archive"},
     {".java",   "text/plain"},
     {".jpeg",   "image/jpeg"},
     {".jpg",    "image/jpeg"},
     {".js", "application/x-javascript"},
     {".log",    "text/plain"},
     {".m3u",    "audio/x-mpegurl"},
     {".m4a",    "audio/mp4a-latm"},
     {".m4b",    "audio/mp4a-latm"},
     {".m4p",    "audio/mp4a-latm"},
     {".m4u",    "video/vnd.mpegurl"},
     {".m4v",    "video/x-m4v"},
     {".mov",    "video/quicktime"},
     {".mp2",    "audio/x-mpeg"},
     {".mp3",    "audio/x-mpeg"},
     {".mp4",    "video/mp4"},
     {".mpc",    "application/vnd.mpohun.certificate"},
     {".mpe",    "video/mpeg"},
     {".mpeg",   "video/mpeg"},
     {".mpg",    "video/mpeg"},
     {".mpg4",   "video/mp4"},
     {".mpga",   "audio/mpeg"},
     {".msg",    "application/vnd.ms-outlook"},
     {".ogg",    "audio/ogg"},
     {".pdf",    "application/pdf"},
     {".png",    "image/png"},
     {".pps",    "application/vnd.ms-powerpoint"},
     {".ppt",    "application/vnd.ms-powerpoint"},
     {".pptx",   "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
     {".prop",   "text/plain"},
     {".rc", "text/plain"},
     {".rmvb",   "audio/x-pn-realaudio"},
     {".rtf",    "application/rtf"},
     {".sh", "text/plain"},
     {".tar",    "application/x-tar"},
     {".tgz",    "application/x-compressed"},
     {".txt",    "text/plain"},
     {".wav",    "audio/x-wav"},
     {".wma",    "audio/x-ms-wma"},
     {".wmv",    "audio/x-ms-wmv"},
     {".wps",    "application/vnd.ms-works"},
     {".xml",    "text/plain"},
     {".z",  "application/x-compress"},
     {".zip",    "application/x-zip-compressed"},
     */

}
